namespace KPIS.GERP.Models.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class update0006 : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.IcmDispense",
                c => new
                    {
                        DispenseId = c.Int(nullable: false, identity: true),
                        DispenseName = c.String(),
                        DispenseDate = c.DateTime(),
                        WhId = c.Int(),
                        FromJobSiteId = c.Int(nullable: false),
                        RequestParcelId = c.Int(nullable: false),
                        ProjectId = c.Int(nullable: false),
                        RequestById = c.Int(nullable: false),
                        Remark = c.String(),
                        ApprovedBy = c.Int(),
                        ApprovedWhen = c.DateTime(),
                        RecordStatus = c.String(maxLength: 2),
                        CreatedBy = c.Int(),
                        CreatedWhen = c.DateTime(),
                        UpdatedBy = c.Int(),
                        UpdatedWhen = c.DateTime(),
                    })
                .PrimaryKey(t => t.DispenseId)
                .ForeignKey("dbo.IcmWarehouse", t => t.WhId)
                .ForeignKey("dbo.hrm_job_site", t => t.FromJobSiteId, cascadeDelete: true)
                .ForeignKey("dbo.hrm_ps_employees", t => t.RequestById, cascadeDelete: true)
                //.ForeignKey("dbo.IcmRequestParcel", t => t.RequestParcelId, cascadeDelete: true)
                .ForeignKey("dbo.bgm_project", t => t.ProjectId, cascadeDelete: true)
                .Index(t => t.WhId)
                .Index(t => t.FromJobSiteId)
                .Index(t => t.RequestById)
                .Index(t => t.RequestParcelId)
                .Index(t => t.ProjectId);
            
            CreateTable(
                "dbo.IcmDispenseHistory",
                c => new
                    {
                        DispenseHisId = c.Int(nullable: false, identity: true),
                        ResItemId = c.Int(),
                        LotNo = c.String(maxLength: 50),
                        WhId = c.Int(),
                        Quantity = c.Int(),
                        Price = c.Decimal(precision: 18, scale: 2),
                        RefNo = c.String(maxLength: 50),
                        RecordStatus = c.String(maxLength: 2),
                        CreatedBy = c.Int(),
                        CreatedWhen = c.DateTime(),
                        UpdatedBy = c.Int(),
                        UpdatedWhen = c.DateTime(),
                        DispenseId = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.DispenseHisId)
                .ForeignKey("dbo.IcmMasResourceItem", t => t.ResItemId)
                .ForeignKey("dbo.IcmWarehouse", t => t.WhId)
                .ForeignKey("dbo.IcmDispense", t => t.DispenseId, cascadeDelete: true)
                .Index(t => t.ResItemId)
                .Index(t => t.WhId)
                .Index(t => t.DispenseId);
            
            CreateTable(
                "dbo.IcmMovement",
                c => new
                    {
                        MovementId = c.Int(nullable: false, identity: true),
                        Title = c.String(nullable: false),
                        ResTypeId = c.Int(nullable: false),
                        RefNo = c.String(),
                        MoveMethod = c.Int(nullable: false),
                        SourceWarehouseId = c.Int(),
                        SourceJobSiteId = c.Int(),
                        DestinationWarehouseId = c.Int(nullable: false),
                        Remark = c.String(),
                        RecordStatus = c.String(maxLength: 2),
                        CreatedBy = c.Int(),
                        CreatedWhen = c.DateTime(),
                        UpdatedBy = c.Int(),
                        UpdatedWhen = c.DateTime(),
                    })
                .PrimaryKey(t => t.MovementId)
                .ForeignKey("dbo.IcmMasResourceType", t => t.ResTypeId, cascadeDelete: true)
                .ForeignKey("dbo.IcmWarehouse", t => t.SourceWarehouseId)
                .ForeignKey("dbo.IcmWarehouse", t => t.DestinationWarehouseId, cascadeDelete: true)
                .ForeignKey("dbo.hrm_job_site", t => t.SourceJobSiteId)
                .Index(t => t.ResTypeId)
                .Index(t => t.SourceWarehouseId)
                .Index(t => t.DestinationWarehouseId)
                .Index(t => t.SourceJobSiteId);
            
            CreateTable(
                "dbo.IcmMovementDtl",
                c => new
                    {
                        MovementDtlId = c.Int(nullable: false, identity: true),
                        MovementId = c.Int(nullable: false),
                        AssetId = c.Int(nullable: false),
                        MoveQty = c.Int(nullable: false),
                        RecordStatus = c.String(maxLength: 2),
                        CreatedBy = c.Int(),
                        CreatedWhen = c.DateTime(),
                        UpdatedBy = c.Int(),
                        UpdatedWhen = c.DateTime(),
                    })
                .PrimaryKey(t => t.MovementDtlId)
                .ForeignKey("dbo.IcmMovement", t => t.MovementId, cascadeDelete: true)
                //.ForeignKey("dbo.IcmAsset", t => t.AssetId, cascadeDelete: true)
                .Index(t => t.MovementId)
                .Index(t => t.AssetId);
            
            CreateTable(
                "dbo.IcmCommittee",
                c => new
                    {
                        CommitteeId = c.Int(nullable: false, identity: true),
                        RefId = c.Int(nullable: false),
                        RefType = c.Int(nullable: false),
                        ComFunctionId = c.Int(),
                        ComPositionId = c.Int(),
                        Code = c.String(nullable: false),
                        ComTypeId = c.Int(),
                        EmpId = c.Int(),
                        EmpName = c.String(nullable: false),
                        EmpPositionName = c.String(),
                        DeleteReason = c.String(),
                        RecordStatus = c.String(),
                        CreatedBy = c.Int(),
                        CreatedWhen = c.DateTime(),
                        UpdatedBy = c.Int(),
                        UpdatedWhen = c.DateTime(),
                    })
                .PrimaryKey(t => t.CommitteeId)
                .ForeignKey("dbo.IcmMasLookUp", t => t.ComFunctionId)
                .ForeignKey("dbo.IcmMasLookUp", t => t.ComPositionId)
                .ForeignKey("dbo.IcmMasLookUp", t => t.ComTypeId)
                .ForeignKey("dbo.IcmMasLookUp", t => t.RefType, cascadeDelete: true)
                .ForeignKey("dbo.hrm_ps_employees", t => t.EmpId)
                .Index(t => t.ComFunctionId)
                .Index(t => t.ComPositionId)
                .Index(t => t.ComTypeId)
                .Index(t => t.RefType)
                .Index(t => t.EmpId);
            
            CreateTable(
                "dbo.IcmFile",
                c => new
                    {
                        FileId = c.Int(nullable: false, identity: true),
                        RefId = c.Int(nullable: false),
                        RefType = c.Int(nullable: false),
                        FileName = c.String(nullable: false),
                        FilePath = c.String(nullable: false),
                        FileType = c.String(nullable: false),
                        FileExtention = c.String(nullable: false),
                        RecordStatus = c.String(),
                        CreatedBy = c.Int(),
                        CreatedWhen = c.DateTime(),
                        UpdatedBy = c.Int(),
                        UpdatedWhen = c.DateTime(),
                        Employee_EmpId = c.Int(),
                    })
                .PrimaryKey(t => t.FileId)
                .ForeignKey("dbo.IcmMasLookUp", t => t.RefType, cascadeDelete: true)
                .ForeignKey("dbo.hrm_ps_employees", t => t.Employee_EmpId)
                .Index(t => t.RefType)
                .Index(t => t.Employee_EmpId);
            
            CreateTable(
                "dbo.IcmMovementHistory",
                c => new
                    {
                        MoveHistId = c.Int(nullable: false, identity: true),
                        AssetId = c.Int(nullable: false),
                        MoveQty = c.Int(nullable: false),
                        SourceLocation = c.String(),
                        DestinationLocation = c.String(),
                        MoveRefNo = c.String(maxLength: 30),
                        MovementType = c.String(maxLength: 50),
                        RecordStatus = c.String(maxLength: 2),
                        CreatedBy = c.Int(nullable: false),
                        CreatedWhen = c.DateTime(nullable: false),
                        UpdatedBy = c.Int(),
                        UpdatedWhen = c.DateTime(),
                    })
                .PrimaryKey(t => t.MoveHistId)
                .ForeignKey("dbo.IcmAsset", t => t.AssetId, cascadeDelete: true)
                .Index(t => t.AssetId);
            
            CreateTable(
                "dbo.IcmDeliveryNoteItemExtra",
                c => new
                    {
                        DnItemExtraId = c.Int(nullable: false, identity: true),
                        DnId = c.Int(),
                        ResItemId = c.Int(),
                        Qty = c.Int(),
                        Price = c.Double(),
                        Descr = c.String(),
                        Remarks = c.String(),
                        CreatedBy = c.Int(),
                        CreatedWhen = c.DateTime(),
                        UpdatedBy = c.Int(),
                        UpdatedWhen = c.DateTime(),
                    })
                .PrimaryKey(t => t.DnItemExtraId)
                .ForeignKey("dbo.IcmDeliveryNote", t => t.DnId)
                .ForeignKey("dbo.IcmMasResourceItem", t => t.ResItemId)
                .Index(t => t.DnId)
                .Index(t => t.ResItemId);
            
            CreateTable(
                "dbo.IcmPurchaseOrderItem",
                c => new
                    {
                        PoItemId = c.Int(nullable: false, identity: true),
                        PoId = c.Int(nullable: false),
                        BidderQuoId = c.Int(),
                        CreatedBy = c.Int(),
                        CreatedWhen = c.DateTime(),
                        UpdatedBy = c.Int(),
                        UpdatedWhen = c.DateTime(),
                    })
                .PrimaryKey(t => t.PoItemId)
                .ForeignKey("dbo.IcmPurchaseOrder", t => t.PoId, cascadeDelete: true)
                .ForeignKey("dbo.IcmBidderQuotation", t => t.BidderQuoId)
                .Index(t => t.PoId)
                .Index(t => t.BidderQuoId);
            
        }
        
        public override void Down()
        {
            DropIndex("dbo.IcmPurchaseOrderItem", new[] { "BidderQuoId" });
            DropIndex("dbo.IcmPurchaseOrderItem", new[] { "PoId" });
            DropIndex("dbo.IcmDeliveryNoteItemExtra", new[] { "ResItemId" });
            DropIndex("dbo.IcmDeliveryNoteItemExtra", new[] { "DnId" });
            DropIndex("dbo.IcmMovementHistory", new[] { "AssetId" });
            DropIndex("dbo.IcmFile", new[] { "Employee_EmpId" });
            DropIndex("dbo.IcmFile", new[] { "RefType" });
            DropIndex("dbo.IcmCommittee", new[] { "EmpId" });
            DropIndex("dbo.IcmCommittee", new[] { "RefType" });
            DropIndex("dbo.IcmCommittee", new[] { "ComTypeId" });
            DropIndex("dbo.IcmCommittee", new[] { "ComPositionId" });
            DropIndex("dbo.IcmCommittee", new[] { "ComFunctionId" });
            DropIndex("dbo.IcmMovementDtl", new[] { "AssetId" });
            DropIndex("dbo.IcmMovementDtl", new[] { "MovementId" });
            DropIndex("dbo.IcmMovement", new[] { "SourceJobSiteId" });
            DropIndex("dbo.IcmMovement", new[] { "DestinationWarehouseId" });
            DropIndex("dbo.IcmMovement", new[] { "SourceWarehouseId" });
            DropIndex("dbo.IcmMovement", new[] { "ResTypeId" });
            DropIndex("dbo.IcmDispenseHistory", new[] { "DispenseId" });
            DropIndex("dbo.IcmDispenseHistory", new[] { "WhId" });
            DropIndex("dbo.IcmDispenseHistory", new[] { "ResItemId" });
            DropIndex("dbo.IcmDispense", new[] { "ProjectId" });
            DropIndex("dbo.IcmDispense", new[] { "RequestParcelId" });
            DropIndex("dbo.IcmDispense", new[] { "RequestById" });
            DropIndex("dbo.IcmDispense", new[] { "FromJobSiteId" });
            DropIndex("dbo.IcmDispense", new[] { "WhId" });
            DropForeignKey("dbo.IcmPurchaseOrderItem", "BidderQuoId", "dbo.IcmBidderQuotation");
            DropForeignKey("dbo.IcmPurchaseOrderItem", "PoId", "dbo.IcmPurchaseOrder");
            DropForeignKey("dbo.IcmDeliveryNoteItemExtra", "ResItemId", "dbo.IcmMasResourceItem");
            DropForeignKey("dbo.IcmDeliveryNoteItemExtra", "DnId", "dbo.IcmDeliveryNote");
            DropForeignKey("dbo.IcmMovementHistory", "AssetId", "dbo.IcmAsset");
            DropForeignKey("dbo.IcmFile", "Employee_EmpId", "dbo.hrm_ps_employees");
            DropForeignKey("dbo.IcmFile", "RefType", "dbo.IcmMasLookUp");
            DropForeignKey("dbo.IcmCommittee", "EmpId", "dbo.hrm_ps_employees");
            DropForeignKey("dbo.IcmCommittee", "RefType", "dbo.IcmMasLookUp");
            DropForeignKey("dbo.IcmCommittee", "ComTypeId", "dbo.IcmMasLookUp");
            DropForeignKey("dbo.IcmCommittee", "ComPositionId", "dbo.IcmMasLookUp");
            DropForeignKey("dbo.IcmCommittee", "ComFunctionId", "dbo.IcmMasLookUp");
            DropForeignKey("dbo.IcmMovementDtl", "AssetId", "dbo.IcmAsset");
            DropForeignKey("dbo.IcmMovementDtl", "MovementId", "dbo.IcmMovement");
            DropForeignKey("dbo.IcmMovement", "SourceJobSiteId", "dbo.hrm_job_site");
            DropForeignKey("dbo.IcmMovement", "DestinationWarehouseId", "dbo.IcmWarehouse");
            DropForeignKey("dbo.IcmMovement", "SourceWarehouseId", "dbo.IcmWarehouse");
            DropForeignKey("dbo.IcmMovement", "ResTypeId", "dbo.IcmMasResourceType");
            DropForeignKey("dbo.IcmDispenseHistory", "DispenseId", "dbo.IcmDispense");
            DropForeignKey("dbo.IcmDispenseHistory", "WhId", "dbo.IcmWarehouse");
            DropForeignKey("dbo.IcmDispenseHistory", "ResItemId", "dbo.IcmMasResourceItem");
            DropForeignKey("dbo.IcmDispense", "ProjectId", "dbo.bgm_project");
            DropForeignKey("dbo.IcmDispense", "RequestParcelId", "dbo.IcmRequestParcel");
            DropForeignKey("dbo.IcmDispense", "RequestById", "dbo.hrm_ps_employees");
            DropForeignKey("dbo.IcmDispense", "FromJobSiteId", "dbo.hrm_job_site");
            DropForeignKey("dbo.IcmDispense", "WhId", "dbo.IcmWarehouse");
            DropTable("dbo.IcmPurchaseOrderItem");
            DropTable("dbo.IcmDeliveryNoteItemExtra");
            DropTable("dbo.IcmMovementHistory");
            DropTable("dbo.IcmFile");
            DropTable("dbo.IcmCommittee");
            DropTable("dbo.IcmMovementDtl");
            DropTable("dbo.IcmMovement");
            DropTable("dbo.IcmDispenseHistory");
            DropTable("dbo.IcmDispense");
        }
    }
}
